﻿using ColorThiefDotNet;
using CommunityToolkit.Mvvm.ComponentModel;
using FanaticRock.Core.Extensions;
using FanaticRock.Core.Models;
using FanaticRock.Helpers;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Media;

namespace FanaticRock.ViewModels
{
    ///<summary>用于展示</summary>
    public partial class DisplaySongViewModel : ObservableObject
    {
        [ObservableProperty]
        public ObservableCollection<string> _comments;

        [ObservableProperty]
        public ObservableCollection<string> _lyrics;

        [ObservableProperty]
        public ResourceSource _coverSource;

        [ObservableProperty]
        public GradientBrush _backgroundBrush;

        [ObservableProperty]
        public GradientBrush _sliderBrush;

        [ObservableProperty]
        public string _title;

        public DisplaySongViewModel()
        {
            TryCreateBackgroundBrush(_coverSource);
        }

        public DisplaySongViewModel(Song song)
        {
            _title = song.Title;
            _coverSource = song.CoverSource;

            TryCreateBackgroundBrush(_coverSource);
        }

        public DisplaySongViewModel(DisplaySongViewModel songViewModel)
        {
            _comments = songViewModel.Comments;
            _lyrics = songViewModel.Lyrics;
            _coverSource = songViewModel.CoverSource;
            _title = songViewModel.Title;

            TryCreateBackgroundBrush(_coverSource);
        }

        private void TryCreateBackgroundBrush(ResourceSource resourceSource)
        {
            try
            {
                CreateBackgroundBrush(resourceSource);
            }
            catch (Exception ex)
            {
                LogExtension.Info($"DisplaySongViewModel.TryCreateBackgroundBrush: {ex}");
            }
        }

        ///<summary>创建背景</summary>
        private async void CreateBackgroundBrush(ResourceSource resourceSource)
        {
            if (_coverSource == null)
            {
                CreateDefaultBackgroundBrush();
                return;
            }

            var bytes = await ResourceSourceHelper.GetBytes(resourceSource);

            var colorThief = new ColorThief();
            var ms = new MemoryStream(bytes);
            var file = new System.Drawing.Bitmap(ms);
            ms.Close();
            ms.Dispose();
            var qcolor = colorThief.GetColor(file);
            file.Dispose();
            var color = qcolor.Color;
            var hsl1 = ColorHelper.ColorConverter.RgbToHsl(new ColorHelper.RGB(color.R, color.G, color.B));
            var hsl2 = new ColorHelper.HSL(hsl1.H, hsl1.S, (byte)(hsl1.L / 2));
            var hsl3 = new ColorHelper.HSL(hsl1.H, hsl1.S, (byte)(hsl1.L / 4));
            var hsl4 = new ColorHelper.HSL(hsl1.H, (byte)(Math.Min(255, hsl1.S + 40)), hsl1.L);

            var color2 = ColorHelper.ColorConverter.HslToRgb(hsl2);
            var color3 = ColorHelper.ColorConverter.HslToRgb(hsl3);
            var color4 = ColorHelper.ColorConverter.HslToRgb(hsl4);

            var c1 = System.Windows.Media.Color.FromRgb(color.R, color.G, color.B);
            var c2 = System.Windows.Media.Color.FromRgb(color2.R, color2.G, color2.B);
            var c3 = System.Windows.Media.Color.FromRgb(color3.R, color3.G, color3.B);

            //var c4 = System.Windows.Media.Color.FromRgb(color4.R, color4.G, color4.B);

            var gb = new LinearGradientBrush()
            {
                StartPoint = new System.Windows.Point(0.5, 0),
                EndPoint = new System.Windows.Point(0.5, 1),
                GradientStops = new GradientStopCollection()
                {
                    new GradientStop(c1, 0),
                    new GradientStop(c2, 0.6),
                    new GradientStop(c3, 1),
                },
            };

            BackgroundBrush = gb;

            SliderBrush = new LinearGradientBrush()
            {
                GradientStops = new GradientStopCollection()
                {
                    new GradientStop(System.Windows.Media.Color.FromArgb(0,color4.R, color4.G, color4.B), 0),
                    new GradientStop(System.Windows.Media.Color.FromArgb(255,color4.R, color4.G, color4.B), 1),
                },
            };
        }

        private void CreateDefaultBackgroundBrush()
        {
            BackgroundBrush = new LinearGradientBrush()
            {
                StartPoint = new System.Windows.Point(0.5, 0),
                EndPoint = new System.Windows.Point(0.5, 1),
                GradientStops = new GradientStopCollection()
                {
                    new GradientStop(System.Windows.Media.Color.FromRgb(64,139,111), 0),
                    new GradientStop(System.Windows.Media.Color.FromRgb(40,87,70), 0.6),
                    new GradientStop(System.Windows.Media.Color.FromRgb(16,36,28), 1),
                },
            };

            SliderBrush = new LinearGradientBrush()
            {
                GradientStops = new GradientStopCollection()
                {
                    new GradientStop(System.Windows.Media.Color.FromArgb(0,64,139,111), 0),
                    new GradientStop(System.Windows.Media.Color.FromArgb(255,64,139,111), 1),
                },
            };
        }

    }
}
